/*
 * 文件名:User.java
 * 创建时间:2009-03-10
 * 版本:1.0
 * 版权所有:杨涛
 */
package com.yangtao.framework.hibernate.entity;

import com.yangtao.framework.common.SystemConstant;
import com.yangtao.framework.hibernate.BaseEntity;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import redis.clients.jedis.Jedis;

import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 基础实体，用户
 *
 * @author 杨涛
 * @version 1.0
 * @创建时间 2009-03-10
 * @修改者
 * @修改时间
 * @修改原因
 * @see User
 */
@Entity()
@Table(name = "sys_user")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends BaseEntity {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 用户代码
	 */
	@Column(name = "user_code", length = 20)
	private String userCode;
	/**
	 * 用户名称
	 */
	@Column(name = "user_name", length = 50)
	private String userName;
	/**
	 * 用户类型
	 */
	@Column(name = "user_type", length = 6)
	private String userType;
	/**
	 * 用户密码
	 */
	@Column(length = 20)
	private String password;
	/**
	 * 电话
	 */
	@Column(length = 20)
	private String telephone;
	/**
	 * 手机
	 */
	@Column(length = 20)
	private String phone;
	/**
	 * E-mail
	 */
	@Column(length = 30)
	private String email;
	/**
	 * 地址1
	 */
	@Column(length = 100)
	private String address1;
	/**
	 * 地址2
	 */
	@Column(length = 100)
	private String address2;
	/**
	 * 地址3
	 */
	@Column(length = 100)
	private String address3;

	/**
	 * 是否启用<br>
	 * 1 是 <br>
	 * 0 否<br>
	 */
	@Column(length = 1)
	private Boolean active;
	/**
	 * 过期日期
	 */
	@Temporal(TemporalType.TIMESTAMP)
	private Date expireDate;

	/**
	 * 最后一次登陆时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	private Date lstLogon;

	/**
	 * 公司ID
	 */
	@Column(length = 20)
	private String companyId;

	/**
	 * 创建时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;

	/**
	 * 创建人Id
	 */
	@Column(length = 20)
	private String createById;

	/**
	 * 创建人名称
	 */
	@Column(length = 50)
	private String createByName;
	/**
	 * 组织机构Id，不能更新，不能修改，仅仅为查询使用
	 */
	@Column(updatable = false, insertable = false)
	private String organizationId;
	/**
	 * 所属组织机构
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "organizationId")
	private Organization organization;

	/**
	 * 用户和用户角色的关系配置，为一对多的配置
	 */
	@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
	@Cascade(CascadeType.ALL)
	private List<UserRole> roles;

	/**
	 * 将用户信息保存在缓存中 ,同时返回缓存中的key
	 *
	 * @return 缓存中的key
	 */
	public String cacheUserInfo(Jedis client) {
		//保存用户信息到redis中
		String sessionId = UUID.randomUUID().toString();
		client.hset(sessionId, SystemConstant.USER_ID, this.id);
		client.hset(sessionId, SystemConstant.USER_NAME, this.userName);
		client.hset(sessionId, SystemConstant.USER_TYPE, this.userType);
		client.hset(sessionId, SystemConstant.COMPANY_ID, this.companyId);
		client.hset(sessionId, SystemConstant.ORGANIZATION_ID, this.organizationId);
		return sessionId;
	}

	/**
	 * @return the userCode
	 */
	public String getUserCode() {
		return userCode;
	}

	/**
	 * @param userCode the userCode to set
	 */
	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the expireDate
	 */
	public Date getExpireDate() {
		return expireDate;
	}

	/**
	 * @param expireDate the expireDate to set
	 */
	public void setExpireDate(Date expireDate) {
		this.expireDate = expireDate;
	}

	/**
	 * @return the lstLogon
	 */
	public Date getLstLogon() {
		return lstLogon;
	}

	/**
	 * @param lstLogon the lstLogon to set
	 */
	public void setLstLogon(Date lstLogon) {
		this.lstLogon = lstLogon;
	}

	/**
	 * @return the userType
	 */
	public String getUserType() {
		return userType;
	}

	/**
	 * @param userType the userType to set
	 */
	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	/**
	 * @return the roles
	 */
	public List<UserRole> getRoles() {
		return roles;
	}

	/**
	 * @param roles the roles to set
	 */
	public void setRoles(List<UserRole> roles) {
		this.roles = roles;
	}

	/**
	 * @return the telephone
	 */
	public String getTelephone() {
		return telephone;
	}

	/**
	 * @param telephone the telephone to set
	 */
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the address1
	 */
	public String getAddress1() {
		return address1;
	}

	/**
	 * @param address1 the address1 to set
	 */
	public void setAddress1(String address1) {
		this.address1 = address1;
	}

	/**
	 * @return the address2
	 */
	public String getAddress2() {
		return address2;
	}

	/**
	 * @param address2 the address2 to set
	 */
	public void setAddress2(String address2) {
		this.address2 = address2;
	}

	/**
	 * @return the address3
	 */
	public String getAddress3() {
		return address3;
	}

	/**
	 * @param address3 the address3 to set
	 */
	public void setAddress3(String address3) {
		this.address3 = address3;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * @return the createById
	 */
	public String getCreateById() {
		return createById;
	}

	/**
	 * @param createById the createById to set
	 */
	public void setCreateById(String createById) {
		this.createById = createById;
	}

	/**
	 * @return the createByName
	 */
	public String getCreateByName() {
		return createByName;
	}

	/**
	 * @param createByName the createByName to set
	 */
	public void setCreateByName(String createByName) {
		this.createByName = createByName;
	}

	public String getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(String organizationId) {
		this.organizationId = organizationId;
	}

	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}
}
